﻿using System;
using System.Data.SqlClient;
using System.Data;
using Oracle.ManagedDataAccess.Client;

namespace Apex.Dapper
{
    /// <summary>
    /// Dapper通道
    /// </summary>
    public static class DapperAccess
    {
        /// <summary>
        /// 获取数据库连接(SqlServer)
        /// </summary>
        /// <param name="address">数据库连接地址</param>
        /// <param name="openTransaction">是否开启事务(默认不开启)</param>
        /// <returns>数据库连接</returns>
        public static IDbConnection GetConnection(string address, bool openTransaction = false)
        {
            IDbConnection dbConnection = new SqlConnection(address);
            if (openTransaction) dbConnection.Open();
            return dbConnection;
        }

        /// <summary>
        /// 获取数据库连接(Oracle)
        /// </summary>
        /// <param name="address">数据库连接地址</param>
        /// <param name="openTransaction">是否开启事务(默认不开启)</param>
        /// <returns>数据库连接</returns>
        [Obsolete]
        public static IDbConnection GetOracleConnection(string address, bool openTransaction = false)
        {
            IDbConnection dbConnection = new OracleConnection(address);
            if (openTransaction) dbConnection.Open();
            return dbConnection;
        }

        /// <summary>
        /// 关闭数据库连接(事务性操作)
        /// </summary>
        /// <param name="dbConnection"></param>
        public static void Close(IDbConnection dbConnection) => dbConnection.Close();
    }
}
